package net.openl10n.mdf;

import net.openl10n.document.api.IDocumentPart;
import net.openl10n.mdf.immutable.Immutable;

class AbstractDocumentPart implements IDocumentPart, Immutable{

	private final Long version;
	private final PartId id;
	
	private final int hashCode;

	AbstractDocumentPart(PartId id, Long version) {
		if(id == null)
			throw new IllegalArgumentException("id cannot be null");
		this.id = id;
		if(version == null)
			throw new IllegalArgumentException("version cannot be null");
		this.version = version;
		
		this.hashCode = generateHashCode();
	}
	
	@Override
	public final Long getVersion() {
		return version;
	}

	@Override
	public final PartId getId() {
		return id;
	}

	@Override
	public boolean equals(Object obj) {
		if(obj == null) return false;
		if( !(obj instanceof AbstractDocumentPart) ) return false;
		AbstractDocumentPart other = (AbstractDocumentPart) obj;
		return ( other.id.equals(id) 
				&& other.version.equals(version)  );
	}
	
	@Override
	public int hashCode() {
		return hashCode;
	}
	
	private int generateHashCode() {
	    int hash = 1;
	    hash = hash * 31 + id.hashCode();
	    hash = hash * 31 + version.hashCode();
	    return hash;
	}

}
